﻿using System;
using System.Web;
using System.Data;
using msBsc.AppLogic.DataAlc;
using System.IO;


namespace msBsc.WebUi
{
    public partial class wfShopCollect : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                //System.Random rd = new Random();
                //sRadom.Value = this.GetRadomString();
                LoadShopDataList(  "", "", "");
            }
        }

        private void BindControls()
        {
            if (!IsPostBack)
            {
            }
        }

        private void LoadShopDataList( string CurrPageNum, string strPageRowsCount, string strSort)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpCustomerShopCollect odataobj = new AppLogic.DataAlc.CBizShpCustomerShopCollect())
            {
                DataSet ds = odataobj.DataObj.QueryBySQL(@"
    SELECT 
     A.SEQ_NUM
     ,A.COMM_TENANT_ID
     ,B.COMM_TENANT_NME
     ,B.LOGO_IMG_URL
     ,A.REMARKS
     ,HOT_GOODS_SUM=SUM(CASE WHEN C.MONTHLY_VOLUME>1 THEN 1 ELSE 0 END)
     ,NEW_GOODS_SUM=SUM(CASE WHEN DATEDIFF(DAY,C.INITIAL_DT,GETDATE())<30 THEN 1 ELSE 0 END)
     FROM SHP_CUSTOMER_SHOP_COLLECT A 
     LEFT OUTER JOIN SHP_COMMERCIAL_TENANT_INFO B ON A.COMM_TENANT_ID=B.COMM_TENANT_ID
     LEFT OUTER JOIN SHP_GOODS_INFO C ON A.COMM_TENANT_ID=C.COMM_TENANT_ID
     WHERE
     A.USER_NME='"+msBsc.SvRef.UserInfo.Operator+@"'
     GROUP BY
     A.SEQ_NUM
     ,A.COMM_TENANT_ID
     ,A.REMARKS
     ,B.COMM_TENANT_NME
     ,B.LOGO_IMG_URL
     ORDER BY SEQ_NUM ASC
    ");

                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", ""
                    , (CurrPageNum == string.Empty ? 1 : int.Parse(CurrPageNum))
                    , (strPageRowsCount == string.Empty ? 10 : int.Parse(strPageRowsCount))
                    , (strSort == string.Empty ? "SEQ_NUM ASC" : strSort));
                this.ExecJScript(@" var data= " + strjsontem1 + @" ; 
                updateTableHtml( data);"); 
            }
        }

        private void deletecoll()
        {
            System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
            transactionOption.Timeout = new TimeSpan(0, 10, 0);
            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
            {
                using (msBsc.AppLogic.DataAlc.CBizShpCustomerShopCollect odataobj = new AppLogic.DataAlc.CBizShpCustomerShopCollect())
                {
                    odataobj.DelCollect(msBsc.SvRef.UserInfo.Operator,this.GetParamHtValue("SHOP_IDS"));
                    ts.Complete();
                }
            } 
        }

        private void loadallgoodsitem()
        {
            string[] strarrShopid = this.GetParamHtValue("SHOP_IDS").Split(',');
            string strdatatype = this.GetParamHtValue("DATA_TP");
            string CurrPageNum = "", strPageRowsCount = "", strSort = "";
            using (Epm.AppLogic.Framework.CDbAccess oacc = new Epm.AppLogic.Framework.CDbAccess())
            {
                for (int i = 0; i < strarrShopid.Length; i++)
                {
                    if (strarrShopid[i].Trim() == string.Empty)
                    {
                        continue;
                    }
                    DataSet ds;
                    if (strdatatype == string.Empty || strdatatype == "0")
                    {
                        ds = oacc.QueryBySQL(@"
    SELECT
    C.IMG_URL
    ,C.GOODS_OLD_PRICE
    ,C.GOODS_STRIKE_PRICE
    ,C.GOODS_ID
    ,C.GOODS_NME
    FROM 
    SHP_GOODS_INFO C
    WHERE
    (C.COMM_TENANT_ID='" + strarrShopid[i] + @"')
    AND (DATEDIFF(DAY,C.INITIAL_DT,GETDATE())<30)
    ORDER BY C.INITIAL_DT DESC
");
                    }
                    else
                    {
                        ds = oacc.QueryBySQL(@"
    SELECT
    C.IMG_URL
    ,C.GOODS_OLD_PRICE
    ,C.GOODS_STRIKE_PRICE
    ,C.GOODS_ID
    ,C.GOODS_NME
    FROM 
    SHP_GOODS_INFO C
    WHERE
    (C.COMM_TENANT_ID='" + strarrShopid[i] + @"')
    AND (C.MONTHLY_VOLUME>1)
    ORDER BY C.MONTHLY_VOLUME DESC
");
                    }
                    string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", ""
                        , (CurrPageNum == string.Empty ? 1 : int.Parse(CurrPageNum))
                        , (strPageRowsCount == string.Empty ? 5 : int.Parse(strPageRowsCount))
                        , (strSort == string.Empty ? "" : strSort));
                    this.ExecJScript(@" var data" + i.ToString() + @"= " + strjsontem1 + @" ; 
                updateGoodsTableHtml('" + strarrShopid[i] + @"', data" + i.ToString() + @");");
                }
            }
        }

        //重构基类的回调处理函数，以strAction属性来判断是什么类型的回调，该变量的值对应前台的ACTION参数，如：按钮的名称等
        public override void InitialByParam()
        {
            if (strAction == "LoadShopDataList")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                LoadShopDataList( this.GetParamHtValue("NEW_PAGE_NUM"), this.GetParamHtValue("PAGE_ROW_COUNT"), this.GetParamHtValue("PAGE_ROW_SORT"));
            }
            else if (strAction == "deletecoll")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                deletecoll();
            }
            else if (strAction == "loadallgoodsitem")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                loadallgoodsitem();
            } 
            //else if (strAction == "btnCancel")//根据传入的回调类型进行处理，这里响应编辑按钮
            //{
            //    SetItemStatus(this.GetParamHtValue("CHECK_ID"), "False");
            //    this.ExecJScript("btnDo('mFinder');");
            //}

            base.InitialByParam();
        } 

    }
}